﻿using System;
using System.Collections.Generic;

namespace Kobets.Automation.Chimera.Common
{
    /// <summary>
    /// Queue that is Observable
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ObservableQueue<T>
    {
        public event EventHandler<EventArgs<T>>  EnQueued;
        public event EventHandler<EventArgs<T>> DeQueued;
        public int Count { get { return _queue.Count; } }

        private readonly Queue<T> _queue = new Queue<T>();

        protected virtual void OnEnqueued(T item)
        {
            if (EnQueued != null)
                EnQueued(this, new EventArgs<T>(item));
        }

        protected virtual void OnDequeued(T item)
        {
            if (DeQueued != null)
                DeQueued(this, new EventArgs<T>(item));
        }

        public virtual void Enqueue(T item)
        {
            _queue.Enqueue(item);
            OnEnqueued(item);
        }

        public virtual T Dequeue()
        {
            var item = _queue.Dequeue();
            OnDequeued(item);
            return item;
        }
    }
}
